package SMUVE;

import static SMUVE.Debug.debug;
import SMUVE.SMUVE_Server.Server_Thread;

public class DataManager {

	ObjectList<Room> rooms = new ObjectList<Room>();
	ObjectList<Player> players = new ObjectList<Player>();
	ObjectList<Item> items= new ObjectList<Item>();
	static final String delimiter = SMUVE.Msg_Pipe.delimiter;
	
	public boolean add_room(Room room) {
		debug("Adding room: " + room);
		boolean retval = rooms.add(room);
		return(retval);
	}

	public boolean add_player(Player player) {
		debug("Adding player: " + player);
		return(players.add(player));
	}
	// possible change from one string to two strings
	
	//keeping portal list? if yes, I didn't do it. Also, other server issues
	public boolean add_portal(Room r1, Room r2) {
		boolean retval=true;
				String room1 = r1.get_name();
				String room2 = r2.get_name();
				retval = retval && r1.add_portal(new Portal(room1));
				retval = retval && r2.add_portal(new Portal(room2));
		

		return(retval);
	}
// Roddy
	public boolean add_item(Item item){
		debug("Adding Item: " + item);
		return(items.add(item));
	}
	//Roddy 
	public boolean remove_room(Room room)
	{
		debug("Removing Room: " + room);
		return rooms.remove(room);
	}
	
	//Roddy 
	public boolean remove_player(Player player)
	{
		debug("Removing Player: " + player);
		return players.remove(player);
	}
	//Roddy 
	
	public boolean remove_item(Item item)
	{
		debug("Removing Item: " + item);
		return items.remove(item);
	}
	
	//Roddy
	public boolean remove_room_portal(Room room1, Room room2)
	{
		debug("Removing Portal: "+ room1.get_name()+"to"+ room2.get_name());
		boolean retvalue=true;
		retvalue= retvalue && room1.remove_portal(room1.get_portal(room2.get_name()));
		retvalue= retvalue && room2.remove_portal(room2.get_portal(room1.get_name()));
		return retvalue;
	}
	
	public String get_current_room(String player) {
		String retval = "";
		Player p = get_player(player);
		if (p != null) {
			retval = p.get_room();
		}

		return(retval);
	}
	public ObjectList<Item> get_player_items(String player) {
		ObjectList<Item> itemList=new ObjectList<Item>();
		Player p = get_player(player);
		for(int i=0; i<items.size(); i++)
		{
			if(items.get(i).get_location().equals(player))
				itemList.add(items.get(i));
		}

		return itemList;
	}


	public boolean move(String player, String room) {
		int i;
		boolean retval = false;

		/* XXX: needs to check that the move is valid */
		for (i = 0; i < players.size(); i++) {
			Player p = players.get(i);
			if (p.get_name().equals(player)) {
				p.set_room(room);
				retval = true;
				break;
			}
		}

		return(retval);
	}

	

	public boolean room_exists(String room) {
		boolean retval = false;

		for (int i = 0; i < rooms.size(); i++) {
			if (rooms.get(i).get_name().equals(room)) {
				retval = true;
				break;
			}
		}

		return(retval);
	}

	// Roddy
	public boolean player_exists(String player)
	{
		boolean retval = false;

		for (int i = 0; i < players.size(); i++) {
			if (players.get(i).get_name().equals(player)) {
				retval = true;
				break;
			}
		}

		return(retval);
	}
	
	//Roddy
	public boolean item_exists(String item)
	{
		boolean retval = false;

		for (int i = 0; i < items.size(); i++) {
			if (items.get(i).get_name().equals(item)) {
				retval = true;
				break;
			}
		}

		return(retval);
	}
	
	
	 public boolean portal_exists(Room r1, Room r2)
	 {
		 Portal p=r1.get_portal(r2.get_name());
		 if(p==null)
			 return false;
		 return true;
	 }
	
	 public Room get_room(String room) {
			Room r = null;

			for (int i = 0; i < rooms.size(); i++) {
				if (rooms.get(i).get_name().equals(room)) {
					r = rooms.get(i);
					break;
				}
			}

			return(r);
		}
	 
	public Player get_player(String name) {
		Player p = null;

		debug("get_player: name = '" + name + "'");
		for (int i = 0; i < players.size(); i++) {
			if (players.get(i).get_name().equals(name)) {
				p = players.get(i);
				break;
			}
		}

		return(p);
	}
	
	public Item get_item(String name) {
		Item item = null;

		debug("get_item: name = '" + name + "'");
		for (int i = 0; i < items.size(); i++) {
			if (items.get(i).get_name().equals(name)) {
				item = items.get(i);
				break;
			}
		}

		return(item);
	}
	
	public boolean lock_item(Item i)
	{
		i.pickedUp();
		return true;
	}
	
	public boolean unlock_item(Item i)
	{
		i.dropped();
		return true;
	}
	
	public ObjectList<Player> get_room_players(Room room)
	{
		ObjectList<Player> playerList=new ObjectList<Player>();
		for(int i=0; i< players.size(); i++)
		{
			if(players.get(i).get_room()==room.get_name())
				playerList.add(players.get(i));
		}
		return playerList;
	}
	public ObjectList<Item> get_room_items(Room room)
	{
		ObjectList<Item> itemList=new ObjectList<Item>();
		for(int i=0; i< items.size(); i++)
		{
			if(items.get(i).get_location()==room.get_name())
				itemList.add(items.get(i));
		}
		return itemList;
	}
	public boolean modify_room(Room room, String newName, String newDescription)
	{
		room.set_name(newName);
		room.set_description(newDescription);
		return true;
	}
	public boolean modify_player(Player player, String newName)
	{
		player.set_name(newName);
		return true;
	}
	public boolean modify_item(Item item, String newName, String newDescription)
	{
		item.set_name(newName);
		item.set_description(newDescription);
		return true;
	}
	// changed room
	public ObjectList<Portal> get_room_portals(Room room)
	{
		return room.getPortals();
	}
	
	public boolean pickUpItem(Player player, Item item)
	{
		return player.pick_up_item(item);
	}
	public boolean dropItem(Player player, Item item)
	{
		return player.drop_item(item);
	}
}
